package com.example.user.service.user;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.IService;
import com.example.framework.common.exception.ServiceException;
import com.example.framework.common.util.EncryptUtils;
import com.example.framework.security.core.LoginUser;
import com.example.user.constants.ErrorCodeConstants;
import com.example.user.constants.SourceTypeEnum;
import com.example.user.controller.auth.vo.UserSourceType;
import com.example.user.controller.basic.vo.*;
import com.example.user.dal.dataobject.user.UserDO;
import org.jetbrains.annotations.NotNull;

import java.util.Set;

/**
 * @author yuzhenxin
 * @date 2023/7/28
 */
public interface UserService extends IService<UserDO> {

    boolean checkUserExists(UserSourceType reqVo);

    UserInfoRespVO getUserInfo(String userId);

    LoginUser loadLoginUserByUserSourceType(UserSourceType userSourceType);

    Set<String> getUserIds();

    boolean updateUserInfo(UserInfoUpdateVO userInfoUpdateVO);

    boolean updateUserName(UserNameUpdateVO reqVO);

    boolean updateUserAvatar(UserAvatarUpdateVO reqVO);

    boolean updateUserPassword(UserPasswordUpdateVO reqVO);

    static LambdaQueryWrapper<UserDO> buildUserQueryWrapper(@NotNull UserSourceType reqVo, String aesSecret) {
        LambdaQueryWrapper<UserDO> lambdaQuery = Wrappers.lambdaQuery();
        String encryptId = EncryptUtils.AesEncrypt(reqVo.getId(), aesSecret);

        Integer source = reqVo.getSource();
        if (source == SourceTypeEnum.SMS.getType()) {
            lambdaQuery.eq(UserDO::getMobile, encryptId);
        } else if (source == SourceTypeEnum.EMAIL.getType()) {
            lambdaQuery.eq(UserDO::getEmail, encryptId);
        } else {
            throw ServiceException.of(ErrorCodeConstants.NOT_IMPLEMENTED);
        }

        return lambdaQuery;
    }

}
